<?php
/**
 * 经纪人客户统计表
 */

namespace app\badmin\model;


use app\common\model\BaseModel;
use think\Db;

class CompanyAgentStatistics extends BaseModel {

    /**
     * 经纪人客户数-累计专属
     * @param $params
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        if ($params['account_type'] == 0) {//公司账号
            $customer_total_number = "(select exclusive_customer_total_number from erp_company_agent_statistics where erp_company_agent_statistics.agent_id=erp_company_agent.id and erp_company_agent.deleted=0 order by day_time desc limit 1 ) as customer_total_number";
            $data = Db::query("select name,{$customer_total_number} from erp_company_agent where company_id = {$params['company_id']} order by customer_total_number desc,id desc limit 5");
        } else {
            $customer_total_number = "(select exclusive_customer_total_number from erp_company_agent_statistics where erp_company_agent_statistics.agent_id=erp_company_agent.id  and erp_company_agent.deleted=0 order by day_time desc limit 1 ) as customer_total_number";
            $data = Db::query("select name,{$customer_total_number} from erp_company_agent where company_shop_id = {$params['company_shop_id']} order by customer_total_number desc,id desc limit 5");
        }
        foreach ($data as $k => $v) {
            if (empty($data[$k]['customer_total_number'])) {
                unset($data[$k]);
            }
        }
        return $data;
    }

}